﻿using System.Windows.Forms;
using System.Diagnostics;

namespace Magellan.ProjectWizards.Setup
{
    public partial class SetupForm : Form
    {
        private readonly SetupModel _model;

        public SetupForm(SetupModel model)
        {
            _model = model;
            InitializeComponent();
            ModelToView();
            CopyRadioYes.CheckedChanged += (x, y) => { ViewToModel(); ModelToView(); };
            CopyRadioNo.CheckedChanged += (x, y) => { ViewToModel(); ModelToView(); };
            TestRadioYes.CheckedChanged += (x, y) => { ViewToModel(); ModelToView(); };
            TestRadioNo.CheckedChanged += (x, y) => { ViewToModel(); ModelToView(); };
        }

        private void ViewToModel()
        {
            _model.CopyLibraries = CopyRadioYes.Checked;
            _model.CreateTestProject = TestRadioYes.Checked;
            _model.LibraryDirectory = DependenciesDirectoryTextBox.Text;
            _model.TestProjectName = TestProjectNameTextBox.Text;
        }

        private void ModelToView()
        {
            CopyRadioYes.Checked = _model.CopyLibraries;
            CopyRadioNo.Checked = !_model.CopyLibraries;
            DependenciesDirectoryTextBox.Text = _model.LibraryDirectory;
            DependenciesDirectoryTextBox.ReadOnly = !_model.CopyLibraries;
            BrowseButton.Enabled = _model.CopyLibraries;
            
            TestRadioYes.Checked = _model.CreateTestProject;
            TestRadioNo.Checked = !_model.CreateTestProject;
            TestProjectNameTextBox.Text = _model.TestProjectName;
            TestProjectNameTextBox.ReadOnly = !_model.CreateTestProject;

            VersionDescription.Text = string.Format("Version: {0}", _model.VersionNumber);
        }

        private void BrowseButtonClicked(object sender, System.EventArgs e)
        {
            FolderDialog.SelectedPath = DependenciesDirectoryTextBox.Text;
            var result = FolderDialog.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                DependenciesDirectoryTextBox.Text = FolderDialog.SelectedPath;
                ViewToModel();
            }
        }

        private void BeforeClose(object sender, FormClosingEventArgs e)
        {
            ViewToModel();

            if (DialogResult == DialogResult.Cancel)
            {
                _model.Cancelled = true;
                return;
            }

            if (!_model.CopyLibraries)
            {
                var copy = MessageBox.Show("Warning: leaving the Magellan libraries in the install location may make it hard to use version control or to collaborate with other developers.", "Magellan", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                if (copy == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }

            if (_model.CreateTestProject && _model.TestProjectName.Trim().Length == 0)
            {
                MessageBox.Show("Please enter the name of the test project.", "Magellan", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;
            }

            if (_model.CopyLibraries && _model.LibraryDirectory.Trim().Length == 0)
            {
                MessageBox.Show("Please select a directory in which to copy the dependencies into.", "Magellan", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;
            }
        }

        private void HandleHelpButtonClicked(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var start = new ProcessStartInfo("http://www.paulstovell.com/magellan");
            start.UseShellExecute = true;
            Process.Start(start);
        }
    }
}